package cn.xz.zgsg.widgets;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import cn.xz.zgsg.R;


public class LinePercentBar extends View {

    private final Paint mPaint; //进度
    private final Paint mBgPaint; //背景
    private int canvasWidth; //画板宽度
    private float mCurData = 0; //当前数字
    private float maxNumber = 100; //最大数字
    private int bigRpm = 100; //转率，因为有些数据是在是太小了，滑动起来太快，这里加一个倍数

    private int startColor = 0; //渐变色，开始颜色
    private int engColor = 0; //渐变色，结束颜色

    public LinePercentBar(Context context) {
        this(context, null);
    }

    public LinePercentBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LinePercentBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.LinePercentBar,defStyleAttr,0);
        //进度条颜色
        int planColor = typedArray.getColor(R.styleable.LinePercentBar_planColor, 0x0000ff);
        //进度条背景颜色
        int planBgColor = typedArray.getColor(R.styleable.LinePercentBar_planBgColor, 0x0000ff);

        //画笔
        mPaint = new Paint();
        mBgPaint = new Paint();
        //防锯齿
        mPaint.setAntiAlias(true);


        //设置颜色
        mPaint.setColor(planColor);
        //设置宽度-宽度为画板宽度
        mBgPaint.setStrokeWidth(canvasWidth);
        //设置颜色
        mBgPaint.setColor(planBgColor);
    }

    /**
     * 动态设置颜色*/
    public void setPaintColor(String s){
        if (s.isEmpty() || s == null){
            mPaint.setColor(Color.parseColor("#FD5B95"));
        }else {
            mPaint.setColor(Color.parseColor(s));
        }
    }
    /**
     * 动态设置渐变色*/
    public void setChangeColor(String s, String e){
        if (s != null && e != null){
            startColor = Color.parseColor(s);
            engColor = Color.parseColor(e);
        }
    }
    /**
     * 强制清零*/
    public void clearNumber(){
        mCurData = 0;
        invalidate();
    }

    /**
     *动态设置数据
     * */
    public void setBigData(float bigNumber,Boolean isBool){
        if (isBool){  //是否进行减速  true 不减速
            bigRpm = 1;
        }

        maxNumber = bigNumber * bigRpm; //获取最大百分比
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {

        //获取画板高度
        //画板高度
        int canvasHeight = getMeasuredHeight();
        //获取画板宽度
        canvasWidth = getMeasuredWidth();

        //画椭圆形
        canvas.drawRoundRect(0,0,canvasWidth, canvasHeight,30,30,mBgPaint);

        //设置渐变色
//        int [] colors = {Color.RED,Color.BLUE};
//        float[] position = {0f,1.0f};
//        LinearGradient linearGradient = new LinearGradient(0,0,getMeasuredWidth(),0,colors,position, Shader.TileMode.CLAMP);
//        mPaint.setShader(linearGradient);
        if (startColor != 0 && engColor != 0){
            mPaint.setShader(new LinearGradient(0,0,getMeasuredWidth(),0,startColor,engColor, Shader.TileMode.CLAMP));
        }

        canvas.drawRoundRect(0,0,canvasWidth/maxNumber * mCurData, canvasHeight,30,30,mPaint);

    }

    public void setPercentData(float data, TimeInterpolator interpolator){
        if (data >= maxNumber/bigRpm){
            data = maxNumber/bigRpm;
        }
        ValueAnimator valueAnimator=ValueAnimator.ofFloat(mCurData,data  * bigRpm );
        //valueAnimator.setDuration((long) (Math.abs(mCurData-data  * bigRpm )*30));
        valueAnimator.setDuration((long) (1000));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float value= (float) valueAnimator.getAnimatedValue();
                mCurData = (float)(Math.round(value*10))/10;
                invalidate();
            }
        });
        valueAnimator.setInterpolator(interpolator);
        valueAnimator.start();
    }
}
